<template>
  <div>
    <div style="height:50px;line-height:50px;background:#eee">
      <span class="iconfont icon-left" style="padding:0 10px" @click="goUpperStory()"></span>
      <span>修改登录密码</span>
    </div>
    <div>
      <van-cell-group>
        <van-field v-model="phone" type="text" label="用户名" placeholder="手机号或者邮箱" required />
        <van-field v-model="code" center clearable label="短信验证码" placeholder="请输入短信验证码" required>
          <van-button
            :disabled="enable"
            slot="button"
            size="small"
            type="primary"
            @click="getVerificationCode()"
          >{{verification_Info}}</van-button>
        </van-field>
        <van-field v-model="password" type="password" label="原密码" placeholder="请输入原密码" required />
        <van-field v-model="newPassword" type="password" label="新密码" placeholder="请输入新密码" required />
        <van-field
          v-model="reNewPassword"
          type="password"
          label="新重复密码"
          placeholder="请输入新重复密码"
          required
        />
      </van-cell-group>
    </div>
    <div>
      <van-button type="primary" block @click="modifyPsd()">确认修改</van-button>
    </div>
  </div>
</template>
<script>
export default {
  data: () => ({
    phone: null,
    password: null,
    code: null,
    num: 300,
    verification_Info: "获取验证码",
    enable: false,
    newPassword: null,
    reNewPassword: null
  }),
  mounted() {},
  methods: {
    goUpperStory() {
      this.$router.go(-1);
    },
    getVerificationCode() {
      let retu = this.phoneFun(this.phone);
      if (this.phone && retu) {
        let data = {
          sendType: "con_smstype_forgetpassword",
          phone: this.phone
        };
        //console.log(data)
        this.$axios.post(`ym/public/member-send-verifycode`, data).then(msg => {
          //console.log(msg);
          if (msg.data.code === 0) {
            this.$Notify({ type: "success", message: msg.data.msg });
            this.enable = true;
            this.countDown();
          } else {
            if (msg.data.data) {
              this.$Notify({ type: "warning", message: msg.data.msg });
              this.enable = true;
              this.num = msg.data.data.residueSecond;
              this.countDown();
            } else if(msg.data.code === 403) {
              this.$Notify({ type: "warning", message: "请重新登录！" });
            }
          }
        });
      } else {
        this.$Notify({
          type: "danger",
          message: "手机号格式不正确,请重新输入!"
        });
      }
    },

    phoneFun(phones) {
      var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
      if (!myreg.test(phones)) {
        return false;
      } else {
        return true;
      }
    },
    countDown() {
      let temp = setTimeout(this.countDown, 1000);
      this.num = this.num - 1;
      this.verification_Info = this.num + "秒后再试";
      if (this.num === 0) {
        // this.verifivationCode = false;
        this.num = 300;
        this.enable = false;
        this.verification_Info = "获取验证码";
        clearTimeout(temp);
      }
    },
    modifyPsd() {
      let data = {
        phone: this.phone,
        phoneCode: this.code,
        oldPassword: this.password,
        password: this.newPassword,
        rePassword: this.reNewPassword
      };
      //console.log(data);
      this.$axios
        .post(`ym/memberuser/member-modify-password`, data)
        .then(msg => {
              //console.log(msg)
          if (msg.data.code === 403) {
            this.$Notify({ type: "success", message: msg.data.msg });
            setTimeout(()=>{
              this.$router.push('/')
            },1000)
          } else {
            this.$Notify({ type: "warning", message: msg.data.msg });
          }
        });
    }
  }
};
</script>